package com.hjm.project.channel.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hjm.common.api.ls.leshua.LeShuaUtil;
import com.hjm.common.api.ls.leshua.entity.AccountAmount;
import com.hjm.common.api.ls.leshua.entity.SubmitWithdraw;
import com.hjm.common.api.ls.leshua.entity.Transfer;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.text.Convert;
import com.hjm.framework.config.CustomConfig;
import com.hjm.project.channel.payment.domain.ChannelPayment;
import com.hjm.project.channel.payment.mapper.ChannelPaymentMapper;
import com.hjm.project.channel.payment.service.IChannelPaymentService;
import com.hjm.strategy.AgentStrategy;
import com.hjm.strategy.AgentStrategyContext;
import com.hjm.strategy.kdb.KdbConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 渠道代理商提现Service业务层处理
 *
 * @author maker-platform
 * @date 2025-03-21
 */
@Service
public class ChannelPaymentServiceImpl implements IChannelPaymentService {
    @Resource
    private ChannelPaymentMapper channelPaymentMapper;
    @Autowired
    private AgentStrategyContext strategyContext;

    /**
     * 查询渠道代理商提现
     *
     * @param id 渠道代理商提现ID
     * @return 渠道代理商提现
     */
    @Override
    public ChannelPayment selectChannelPaymentById(Integer id) {
        return channelPaymentMapper.selectChannelPaymentById(id);
    }

    /**
     * 查询渠道代理商提现列表
     *
     * @param channelPayment 渠道代理商提现
     * @return 渠道代理商提现
     */
    @Override
    public List<ChannelPayment> selectChannelPaymentList(ChannelPayment channelPayment) {
        return channelPaymentMapper.selectChannelPaymentList(channelPayment);
    }

    /**
     * 查询渠道代理商提现列表
     *
     * @param channelPayment 渠道代理商提现
     * @return 渠道代理商提现 一个结果
     */
    @Override
    public ChannelPayment selectChannelPaymentOne(ChannelPayment channelPayment) {
        return channelPaymentMapper.selectChannelPaymentOne(channelPayment);
    }

    /**
     * 新增渠道代理商提现
     *
     * @param channelPayment 渠道代理商提现
     * @return 结果
     */
    @Override
    public int insertChannelPayment(ChannelPayment channelPayment) {
        channelPayment.setCreatetime(DateUtils.getNowDate());
        channelPayment.setStatus(0);
        channelPayment.setInvoiceType("1");//扣税模式 0：代开 1：扣发票 2:扣子代发票(需要申请) (account_type为0时才生效，默认为扣发票)
        channelPayment.setBusinessNo("X" + System.currentTimeMillis());//提现流水
        if (StringUtils.equals(channelPayment.getOperationType(), "2")) {
            channelPayment.setAgentId(channelPayment.getApplyAgentId());
            channelPayment.setApplyAgentId(channelPayment.getDstAgentId());
        } else {
            if (StringUtils.equals(channelPayment.getChannelCode(), "LS")) {
                if (StringUtils.equals(channelPayment.getApplyAgentId(), CustomConfig.getSubAgentId())) {
                    channelPayment.setAgentId(CustomConfig.getAgentId());
                } else {
                    channelPayment.setAgentId(CustomConfig.getSubAgentId());
                }
            }else {
                channelPayment.setAgentId(KdbConfig.AGENT_CODE);
            }
        }

        int row = channelPaymentMapper.insertChannelPayment(channelPayment);
        if (row > 0) {
            AgentStrategy strategy = strategyContext.getStrategy(channelPayment.getChannelCode());
            if (StringUtils.equals(channelPayment.getOperationType(), "1")) {
                strategy.withdraw(channelPayment);
                channelPaymentMapper.updateChannelPayment(channelPayment);
            } else if (StringUtils.equals(channelPayment.getOperationType(), "2")) {
                strategy.transfer(channelPayment);
                channelPaymentMapper.updateChannelPayment(channelPayment);
            }
        }
        return row;
    }

    /**
     * 修改渠道代理商提现
     *
     * @param channelPayment 渠道代理商提现
     * @return 结果
     */
    @Override
    public int updateChannelPayment(ChannelPayment channelPayment) {
        return channelPaymentMapper.updateChannelPayment(channelPayment);
    }

    /**
     * 删除渠道代理商提现对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteChannelPaymentByIds(String ids) {
        return channelPaymentMapper.deleteChannelPaymentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除渠道代理商提现信息
     *
     * @param id 渠道代理商提现ID
     * @return 结果
     */
    @Override
    public int deleteChannelPaymentById(String id) {
        return channelPaymentMapper.deleteChannelPaymentById(id);
    }
}
